package com.dp.appi.util;

/**
 * Created by bchen on 2015/4/17.
 * 编码自增
 */

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

public class GenIndexUtil {

    private static  final int LEN = 5;

    //读取资源文件,并处理中文乱码
    public static void readPropertiesFile(String filename) {
        Properties properties = new Properties();
        try {
            InputStream inputStream = new FileInputStream(filename);
            properties.load(inputStream);
            inputStream.close(); //关闭流
        } catch (IOException e) {
            e.printStackTrace();
        }
        String chinese = properties.getProperty("chinese");
        try {
            chinese = new String(chinese.getBytes("ISO-8859-1"), "GBK"); // 处理中文乱码
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    /**
     * 编码自增1
     * @param zk
     * @return
     */
    public static  String getIndex(String zk){
        synchronized (GenIndexUtil.class){
            return hanldIndex(zk);
        }
    }

    /**
     * 编码自增2
     * @param zk
     * @return
     */
    public static String hanldIndex(String zk) {
        int index = 0;
        String datei = "";
        try{
            String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();    //添加


            if("\\".equals(File.separator)){
                path = path.replace("/", "\\\\");
                if(path.indexOf(".jar")!=-1){
                    path = path.substring(0,path.lastIndexOf("\\\\"));
                    path = path.substring(5,path.lastIndexOf("\\\\"));
                    path = path+"\\\\gen-index.properties";
                }else{
                    path = path+"gen-index.properties";
                }
            }
            //linux下
            if("/".equals(File.separator)){
                path = path.replace("\\", "/");
                if(path.indexOf(".jar")!=-1){
                    path = path.substring(0,path.lastIndexOf("/"));
                    path = path.substring(5,path.lastIndexOf("/"));
                    path = path+"/gen-index.properties";
                }else{
                    path = path+"/gen-index.properties";
                }
            }
            File af=new File(path);

            Properties properties = new Properties();
            InputStream inputStream = new FileInputStream(af.getPath());
            properties.load(inputStream);
            inputStream.close(); //关闭流

            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String d = sdf.format(new Date());

            if(properties.getProperty("date")!=null){
                datei = properties.getProperty("date");
            }
            if(datei==""||Integer.valueOf(d)>Integer.valueOf(datei)){
                datei = d;
            }
            properties.setProperty("date", datei);

            if(properties.getProperty(zk)!=null) {
                index = Integer.valueOf(properties.getProperty(zk)) + 1;
            }else{
                index = 1;
            }
            properties.setProperty(zk,String.valueOf(index));

            OutputStream outputStream = new FileOutputStream(af.getPath());
            properties.store(outputStream, "author: bchen121@163.com");
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        String ix = String.valueOf(index);
        if(ix.length()<LEN){
            int l = LEN-ix.length();
            for(int i=0;i<l;i++){
                ix = "0"+ix;
            }
        }
        return zk+datei+ix;
    }

    //写资源文件，含中文
    public static void writePropertiesFile(String filename) {
        Properties properties = new Properties();
        try {
            OutputStream outputStream = new FileOutputStream(filename);
            properties.store(outputStream, "author: bchen121@163.com");
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        System.out.println(getIndex("ss"));
    }

}
